//
//  BottomGoodsCollectionViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/5.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "BottomGoodsCollectionViewCell.h"
#import "MarkLabel.h"

@interface BottomGoodsCollectionViewCell ()

@property (strong, nonatomic) UIView *goodBgView;
@property (strong, nonatomic) UIImageView *goodIV;
// 券(带金额)
@property (strong, nonatomic) UIImageView *vouchersIV;
@property (strong, nonatomic) UILabel *voucherMoneyL;

@property (strong, nonatomic) UIView *lineV;

// 标题
@property (strong, nonatomic) UILabel *nameL;
// 自营
@property (strong, nonatomic) MarkLabel *proprietaryL;
//券（不带金额， 小图标）
//@property (strong, nonatomic) MarkLabel *smallVouchersL;
//@property (strong, nonatomic) MarkLabel *integralL;

// 销量
@property (strong, nonatomic) UILabel *sellOutL;
@property (strong, nonatomic) UILabel *sellOutNumberL;
// 折扣价
@property (strong, nonatomic) UILabel *currentPriceL;
@property (strong, nonatomic) UILabel *oldPriceL;

@end

@implementation BottomGoodsCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {
        
        [self.contentView addSubview:self.goodBgView];
        [self.contentView addSubview:self.goodIV];
        [self.contentView addSubview:self.vouchersIV];
        
        [self.contentView addSubview:self.voucherMoneyL];
        [self.contentView addSubview:self.lineV];
        [self.contentView addSubview:self.nameL];
        [self.contentView addSubview:self.proprietaryL];
//        [self.contentView addSubview:self.smallVouchersL];
//        [self.contentView addSubview:self.integralL];
        [self.contentView addSubview:self.sellOutL];
        [self.contentView addSubview:self.sellOutNumberL];
        [self.contentView addSubview:self.currentPriceL];
        [self.contentView addSubview:self.oldPriceL];
        
    }
    
    return self;
}

- (void)setJdModel:(JDShoppingListModel *)jdModel {
    _jdModel = jdModel;
//    self.smallVouchersL.text = @"JD";
    self.vouchersIV.hidden = YES;
    self.voucherMoneyL.hidden = YES;
    [self.goodIV sd_setImageWithURL:[NSURL URLWithString:jdModel.url]];
    self.sellOutNumberL.text = [NSString stringWithFormat:@"%@件", jdModel.yishou];
    self.nameL.text = jdModel.title;
    NSString *oldPrice = [NSString stringWithFormat:@"￥%@", jdModel.goods_pricey];
    NSDictionary *attributeDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *oldPirceAttributeStr = [[NSMutableAttributedString alloc] initWithString:oldPrice attributes:attributeDic];
    self.oldPriceL.attributedText = oldPirceAttributeStr;
    
    NSString *price = [NSString stringWithFormat:@"￥%@",jdModel.goods_price];
    NSMutableAttributedString *priceAttributeStr = [[NSMutableAttributedString alloc] initWithString:price];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(30) range:NSMakeRange(1, price.length - 1)];
    self.currentPriceL.attributedText = priceAttributeStr;
    
//    self.integralL.text = [NSString stringWithFormat:@"趣豆返%@", jdModel.commission_rate];
    
    self.proprietaryL.hidden = YES;
//    self.smallVouchersL.hidden = NO;
//    self.integralL.hidden = NO;
//
//    [self.smallVouchersL sizeToFit];
//    [self.integralL sizeToFit];
}

- (void)setModel:(TbGoodsListModel *)model {
    
    _model = model;
//    self.smallVouchersL.text = @"券";
    [self.goodIV sd_setImageWithURL:[NSURL URLWithString:model.pict_url]];
    self.vouchersIV.hidden = NO;
    self.voucherMoneyL.hidden = NO;
    NSString *vouchNum = [NSString stringWithFormat:@"￥%@", model.coupon_amount];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:vouchNum];
    [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:jisuan(30)] range:NSMakeRange(1, vouchNum.length - 1)];
    self.voucherMoneyL.attributedText = attributeStr;
    
    self.nameL.text = model.title;
    
    self.sellOutNumberL.text = [NSString stringWithFormat:@"%@件", model.volume];
    
    NSString *oldPrice = [NSString stringWithFormat:@"￥%@", model.goods_price];
    NSDictionary *attributeDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *oldPirceAttributeStr = [[NSMutableAttributedString alloc] initWithString:oldPrice attributes:attributeDic];
    self.oldPriceL.attributedText = oldPirceAttributeStr;
    
    NSString *price = [NSString stringWithFormat:@"￥%@",model.zk_final_price];
    NSMutableAttributedString *priceAttributeStr = [[NSMutableAttributedString alloc] initWithString:price];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(30) range:NSMakeRange(1, price.length - 1)];
    self.currentPriceL.attributedText = priceAttributeStr;
    
//    self.integralL.text = [NSString stringWithFormat:@"趣豆返%@", model.commission_rate];
    
    self.proprietaryL.hidden = YES;
//    self.smallVouchersL.hidden = NO;
//    self.integralL.hidden = NO;
//
//    [self.smallVouchersL sizeToFit];
//    [self.integralL sizeToFit];
  
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.goodBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.contentView.mas_top);
        make.centerX.mas_equalTo(weakSelf.contentView.mas_centerX);
        make.width.mas_equalTo(jisuan(350));
        make.height.mas_equalTo(jisuan(350));
        
    }];
    
    [self.goodIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.goodBgView.mas_left).with.offset(jisuan(10));
        make.right.mas_equalTo(weakSelf.goodBgView.mas_right).with.offset(-jisuan(10));
        make.top.mas_equalTo(weakSelf.goodBgView.mas_top).with.offset(jisuan(10));
        make.bottom.mas_equalTo(weakSelf.goodBgView.mas_bottom).with.offset(-jisuan(10));
    }];
    
    [self.vouchersIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-jisuan(10));
        make.bottom.mas_equalTo(weakSelf.goodIV.mas_bottom).with.offset(-jisuan(10));
        make.width.mas_equalTo(jisuan(160));
        make.height.mas_equalTo(jisuan(60));
    }];
    
    [self.voucherMoneyL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.vouchersIV.mas_centerY);
        make.right.mas_equalTo(weakSelf.vouchersIV.mas_right).with.offset(-jisuan(60));
        make.width.mas_equalTo(jisuan(95));
        make.height.mas_equalTo(jisuan(60));
    }];
    
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left);
        make.right.mas_equalTo(weakSelf.contentView.mas_right);
        make.top.mas_equalTo(weakSelf.goodBgView.mas_bottom);
        make.height.mas_equalTo(jisuan(1));
        
    }];
    [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(jisuan(10));
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-jisuan(10));
        make.top.mas_equalTo(weakSelf.lineV.mas_bottom).with.offset(jisuan(10));
        
    }];
    
    [self.currentPriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(jisuan(10));
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(-jisuan(10)); // 20
        
    }];
    
    [self.oldPriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.currentPriceL.mas_right).with.offset(Margin);
        make.bottom.mas_equalTo(weakSelf.currentPriceL.mas_bottom);
    
    }];
    
//    [self.smallVouchersL mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(jisuan(10));
//        make.bottom.mas_equalTo(weakSelf.currentPriceL.mas_top).with.offset(-jisuan(10));
//    }];

//    [self.integralL mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(weakSelf.smallVouchersL.mas_right).with.offset(jisuan(10));
//        make.bottom.mas_equalTo(weakSelf.currentPriceL.mas_top).with.offset(-jisuan(10));
//    }];

    [self.sellOutNumberL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.currentPriceL.mas_top).with.offset(-jisuan(10));
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-jisuan(10));
    }];
    
    [self.sellOutL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.currentPriceL.mas_top).with.offset(-jisuan(10));
        make.right.mas_equalTo(weakSelf.sellOutNumberL.mas_left);
    }];
    
}

#pragma mark -lazy
- (UIView *)goodBgView {
    
    if (!_goodBgView) {
    
        _goodBgView = [[UIView alloc] init];
        _goodBgView.backgroundColor = WhiteColor;
    }
    
    return _goodBgView;
}

- (UIImageView *)goodIV {
    
    if (!_goodIV) {
        _goodIV = [[UIImageView alloc] init];
        _goodIV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _goodIV;
}

- (UIImageView *)vouchersIV {
    
    if (!_vouchersIV) {
        _vouchersIV = [[UIImageView alloc] init];
        _vouchersIV.contentMode = UIViewContentModeScaleAspectFit;
        _vouchersIV.image = [UIImage imageNamed:@"vuochersBG"];
    }
    return _vouchersIV;
}

- (UILabel *)voucherMoneyL {
    
    if (!_voucherMoneyL) {
        
        _voucherMoneyL = [[UILabel alloc] init];
        _voucherMoneyL.textColor = Colorff0000;
        _voucherMoneyL.textAlignment = NSTextAlignmentCenter;
        
        NSString *moneyStr = @"￥200";
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:moneyStr];
        [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
        [attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:jisuan(30)] range:NSMakeRange(1, moneyStr.length - 1)];
        _voucherMoneyL.attributedText = attributeStr;
        
        [_voucherMoneyL sizeToFit];
        
    }
    
    return _voucherMoneyL;
}

- (UIView *)lineV {
    
    if (!_lineV) {
        
        _lineV = [[UIView alloc] init];
        _lineV.backgroundColor = Colordddddd;
    }
    
    return _lineV;
}

- (UILabel *)nameL {
    
    if (!_nameL) {
        
        _nameL = [[UILabel alloc] init];
        _nameL.textColor = Color333333;
        _nameL.font = Font(24);
        _nameL.numberOfLines = 2;
        _nameL.lineBreakMode = NSLineBreakByTruncatingTail;

        
    }
    
    return _nameL;
    
}

- (MarkLabel *)proprietaryL {
    
    if (!_proprietaryL) {
        
        _proprietaryL = [[MarkLabel alloc] init];
        _proprietaryL.textColor = Colorff0000;
        _proprietaryL.font = Font(20);
        _proprietaryL.textAlignment = NSTextAlignmentCenter;
        
        _proprietaryL.text = @"自营";
        _proprietaryL.layer.borderColor = Colorff0000.CGColor;
        _proprietaryL.layer.cornerRadius = jisuan(16);
        _proprietaryL.layer.borderWidth = jisuan(2);
        
    }
    
    return _proprietaryL;
}

//- (MarkLabel *)smallVouchersL {
//
//    if (!_smallVouchersL) {
//
//        _smallVouchersL = [[MarkLabel alloc] init];
//        _smallVouchersL.textColor = Colorff0000;
//        _smallVouchersL.font = Font(20);
//        _smallVouchersL.textAlignment = NSTextAlignmentCenter;
//
//        _smallVouchersL.text = @"券";
//        _smallVouchersL.edgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
//        _smallVouchersL.layer.borderWidth = jisuan(2);
//        _smallVouchersL.layer.borderColor = Colorff0000.CGColor;
//        _smallVouchersL.layer.cornerRadius = jisuan(10);
//        _smallVouchersL.layer.masksToBounds = YES;
//
//    }
//
//    return _smallVouchersL;
//}

//- (MarkLabel *)integralL {
//
//    if (!_integralL) {
//
//        _integralL = [[MarkLabel alloc] init];
//        _integralL.textColor = Colorff0000;
//        _integralL.font = Font(20);
//        _integralL.textAlignment = NSTextAlignmentCenter;
//
//        _integralL.text = @"积分抵扣";
//        _integralL.edgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
//        _integralL.layer.borderWidth = jisuan(2);
//        _integralL.layer.borderColor = Colorff0000.CGColor;
//        _integralL.layer.cornerRadius = jisuan(10);
//        _integralL.layer.masksToBounds = YES;
//
//    }
//
//    return _integralL;
//}

- (UILabel *)sellOutL {
    
    if (!_sellOutL) {
        
        _sellOutL = [[UILabel alloc] init];
        _sellOutL.textColor = Color999999;
        _sellOutL.font = Font(20);
        _sellOutL.textAlignment = NSTextAlignmentLeft;
        
        _sellOutL.text = @"已售：";
        
    }
    
    return _sellOutL;
    
}

- (UILabel *)sellOutNumberL {
    
    if (!_sellOutNumberL) {
        
        _sellOutNumberL = [[UILabel alloc] init];
        _sellOutNumberL.textColor = Color999999;
        _sellOutNumberL.font = Font(20);
        _sellOutNumberL.textAlignment = NSTextAlignmentLeft;
        
//        _sellOutNumberL.text = @"1000件";
        
    }
    
    return _sellOutNumberL;
    
}

- (UILabel *)currentPriceL {
    
    if (!_currentPriceL) {
        
        _currentPriceL = [[UILabel alloc] init];
        _currentPriceL.textColor = Colorff0000;
        _currentPriceL.textAlignment = NSTextAlignmentLeft;
    
    }
    
    return _currentPriceL;
    
}

- (UILabel *)oldPriceL {
    
    if (!_oldPriceL) {
        
        _oldPriceL = [[UILabel alloc] init];
        _oldPriceL.textColor = Color999999;
        _oldPriceL.font = Font(20);
        _oldPriceL.textAlignment = NSTextAlignmentLeft;

    }
    
    return _oldPriceL;
    
}


@end
